package com.dreamoo.conf;

import com.dreamoo.aicommon.constant.SystemMessage;
import com.dreamoo.aitool.TestTool;
import com.dreamoo.service.ai.DreamAnalyst;
import com.dreamoo.service.ai.OpenAiChatService;
import dev.langchain4j.community.web.search.searxng.SearXNGWebSearchEngine;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
import dev.langchain4j.rag.content.retriever.WebSearchContentRetriever;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.store.memory.chat.redis.RedisChatMemoryStore;
import dev.langchain4j.web.search.WebSearchTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 山顶洞人郑某
 * @className AiServicesConfig.java 高级AI服务类配置
 * @date 2025 04 29
 */
@Configuration
public class AiServicesConfig {

    /**
     * 流式对话模型
     */
    @Autowired
    private OpenAiStreamingChatModel openAiStreamingChatModel;

    @Autowired
    private OpenAiChatModel openAiChatModel;
    @Autowired
    private SearXNGWebSearchEngine searXNGWebSearchEngine;

    /**
     * AI梦境分析师 高级服务
     * @return 梦境分析师的代理对象
     */
    @Bean
    public DreamAnalyst dreamAnalyst() {
        return AiServices.builder(DreamAnalyst.class)
                .streamingChatLanguageModel(openAiStreamingChatModel)
                .chatLanguageModel(openAiChatModel)
                .systemMessageProvider(o ->SystemMessage.DREAM_ANALYST)
                .tools(WebSearchTool.from(searXNGWebSearchEngine)) //设置联网搜索工具
                .contentRetriever(WebSearchContentRetriever.builder() //设置联网搜索的条件
                        .maxResults(5) //最大搜索结果数
                        .webSearchEngine(searXNGWebSearchEngine) //本地docker部署的搜索引擎
                        .build())
                //聊天窗口设置为Redis存储
                .chatMemoryProvider(new ChatMemoryProvider() {
                    @Override
                    public ChatMemory get(Object o) {
                        return MessageWindowChatMemory.builder()
                                .id(o)
                                .maxMessages(10)
                                .chatMemoryStore(RedisChatMemoryStore.builder()
                                        .port(6379)
                                        .host("127.0.0.1")
                                        .build())
                                .build();
                    }
                })
                .build();
    }



    @Bean
    public OpenAiChatService openAiChatService() {
        return AiServices.builder(OpenAiChatService.class)
                .streamingChatLanguageModel(openAiStreamingChatModel)
                .systemMessageProvider(o -> SystemMessage.OPEN_AI_CHAT_SERVICES_MESSAGE) //系统提示语
                .tools(new TestTool(), WebSearchTool.from(searXNGWebSearchEngine))
                .contentRetriever(WebSearchContentRetriever.builder() //设置联网搜索的条件
                        .maxResults(5) //最大搜索结果数
                        .webSearchEngine(searXNGWebSearchEngine) //本地docker部署的搜索引擎
                        .build())
                //聊天窗口设置为Redis存储
                .chatMemoryProvider(new ChatMemoryProvider() {
                    @Override
                    public ChatMemory get(Object o) {  //O 是每个聊天窗的ID
                        return MessageWindowChatMemory.builder()
                                .maxMessages(10)
                                .id(o)
                                .chatMemoryStore(RedisChatMemoryStore
                                        .builder()
                                        .port(6379)
                                        .host("127.0.0.1")
                                        .build()
                                )
                                .build();
                    }
                })
                .build();
    }




}
